%include "pm.inc"
org 07c00h
jmp LABEL_BEGIN

;接下来的是gdt表部分，在该程序运行的过程中，cr0寄存器被置位，接下来，对于每一个寻址如BASE:OFFSET，base都会从gdt里面寻找到对应的物理段地址，再结合OFFSET找到对应的物理地址
[SECTION .gdt]
LABEL_GDT: Descriptor 0,0,0
LABEL_DESC_CODE32: Descriptor 0,SegCode32Len -1,DA_C+DA_32
LABEL_DESC_VIDEO: Descriptor 0B8000h,0ffffh,DA_DRW
GdtLen equ $-LABEL_GDT
GdtPtr dw GdtLen-1
			dd 0
SelectorCode32 equ LABEL_DESC_CODE32 - LABEL_GDT
SelectorVideo equ LABEL_DESC_VIDEO - LABEL_GDT

[SECTION .s16]
[BITS 16]
LABEL_BEGIN:
	mov ax,cs
	mov ds,ax
	mov es,ax
	mov ss,ax
	mov sp,0100h

	xor eax,eax
	mov ax,cs
	shl eax,4
	add eax,LABEL_SEG_CODE32
	mov [LABEL_DESC_CODE32+2],ax
	shr eax,16
	mov byte [LABEL_DESC_CODE32+4],al
	mov byte [LABEL_DESC_CODE32+7],ah

	xor eax,eax
	mov ax,ds
	shl eax,4
	add eax,LABEL_GDT
	mov dword [GdtPtr+2],eax

	lgdt [GdtPtr]

	cli

	in al,92h
	or al,00000010b
	out 92h,al

	mov eax,cr0
	or eax,1
	mov cr0,eax

	jmp dword SelectorCode32:0

[SECTION .s32]
[BITS 32]
LABEL_SEG_CODE32:
	mov ax,SelectorVideo
	mov gs,ax
	mov edi,(80*11+79)*2
	mov ah,0ch
	mov al,'P'
	mov [gs:edi],ax

	jmp $
SegCode32Len equ $ - LABEL_SEG_CODE32

;------------------------------------------
;nasm 02_protect_mode.asm -o 02_protect_mode.bin
;dd if=02_protect_mode.bin of=02_protect_mode.img bs=512 count=1 conv=notrunc
;利用nasm编译该代码文件生成bin文件
;在生成的映像文件1fe和1ff改为0x55aa
;然后再将该bin文件拷贝到img的映像上面，最后在bochs上运行
;------------------------------------------